package neu.edu.cn.nuemall.controller;

import neu.edu.cn.nuemall.entity.Userinfo;
import neu.edu.cn.nuemall.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.List;

@Controller
@RequestMapping("/edge")
public class EdgeController {

    @Autowired
    private UserinfoService userinfoService;

    @RequestMapping("/register")
    public String toRegister(){
        return "edge/register";
    }
    @RequestMapping("/register-complete")
    public String registerComplete(Userinfo userInfo){
        int i=userinfoService.addUser(userInfo);
        if (i>0){
            return "/edge/register-complete";
        }
        return "forward:/edge/register";
    }

    @RequestMapping("/login")
    public String toLogin(){
        return "/edge/login";
    }
    @RequestMapping("/handler/login")
    public String loginComplete(Userinfo userinfo, HttpSession session, Model model){
        List<Userinfo> userinfoList = userinfoService.getUserList(userinfo);
        if (!CollectionUtils.isEmpty(userinfoList)) {
            session.setAttribute("USER_INFO", userinfoList.get(0));
            return "forward:/";
        }
        model.addAttribute("msg","账号或密码错误，请重新输入");
        //返回到登录页面
        return "forward:/edge/login";

    }



    @RequestMapping("/logout")
    public String registerUser(HttpSession session){
        session.removeAttribute("USER_INFO");
        return "forward:/";
    }
}
